﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReviewCurveApp.Classes;
using System.Data;

namespace ReviewCurveApp.Contorller
{
    public class ReviewsManager
    {
        DBLoader db = new DBLoader();

        internal bool HasDone(Task task)
        {
            string UnitId=task.UnitId.ToString();

            string Sequence = task.Sequnce.ToString();

            string Key = MakeKey(UnitId,Sequence);

            if (RecordCacheDC.ContainsKey(Key))
            {
                TaskStatus status = RecordCacheDC[Key];
                
                if (status==TaskStatus.Finished)
                {
                    return true;
                }
            }
            return false;

        }

        Dictionary<string, TaskStatus> RecordCacheDC = new Dictionary<string, TaskStatus>();

        internal void LoadReviewRecords(DateTime dateTime)
        {
            RecordCacheDC.Clear();
            
            MyDBCommand command = new MyDBCommand();

            command.Sql= string.Format(
                "select  *  from test.reviews "
                //+ " where CompletedDatetime=@DateTime "
                  );
            //command.Parameters.Add("@DateTime", dateTime);
           
            DataTable dt = db.Query(command);

            if (dt == null || dt.Rows.Count <= 0)
            {
                return ;
            }
            
            

            foreach (DataRow dr in dt.Rows)
            {
                AddToCache(dr);

            }
            
        }

        private void AddToCache(DataRow dr)
        {
            string UnitId=DbHelper.GetStringFromRow("UnitId", dr);

            string Sequence=DbHelper.GetStringFromRow("Sequence", dr);

            string Key = MakeKey(UnitId, Sequence);

            TaskStatus status = (TaskStatus)DbHelper.GetIntValueFromRow("status", dr);

            if (!RecordCacheDC.ContainsKey(Key))
            {
                RecordCacheDC.Add(Key, status);
            }


        }

        private string MakeKey(string UnitId,string Sequence)
        {
            return UnitId + "-" + Sequence;
        }
    }
}
